package com.liukai.personalbills.controller;

import com.liukai.personalbills.entity.User;
import com.liukai.personalbills.entity.UserMessage;
import com.liukai.personalbills.service.UserService;
import com.liukai.personalbills.util.TextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/login")
//    @RequestMapping("/login/{username}/{password}")
    public UserMessage login(@RequestParam String username, @RequestParam String password){

        String result = TextUtil.LOGIN_FAIL_NO_USER;
        String code = TextUtil.CODE_NO;
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        result = userService.login(user);
        if (result.equals(TextUtil.LOGIN_SUCCESS)){
            code = TextUtil.CODE_OK;
        }
        UserMessage userMessage = new UserMessage(code,result);
        System.out.println(result);

        return userMessage;
    }

    @PostMapping("/register")
    public UserMessage register(@RequestParam String username,@RequestParam String password){

        String result = TextUtil.REGISTER_FAIL_USER_EXIST;
        String code = TextUtil.CODE_NO;
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        result = userService.register(user);
        if (result.equals(TextUtil.REGISTER_SUCCESS)){
            code = TextUtil.CODE_OK;
        }
        UserMessage userMessage = new UserMessage(code,result);
        System.out.println(result);

        return userMessage;
    }

    @RequestMapping("/update")
    public String update(@RequestParam String username,@RequestParam String password){

        String result = TextUtil.UPDATE_FAIL;
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        result = userService.update(user);
        System.out.println(result);

        return result;
    }

    @RequestMapping("/hello")
    public String hello(){
        return "Hello Spring Boot";
    }

}
